<template>
  <div class="container" ref="mainCon">
    <div class="mainCon">
      <el-row class="searchBox searchInputWidth">
        <el-form :inline="true" :model="filterField">
          <el-col :lg="8" :xl="6">
            <el-form-item label="项目名称" class="row-padding-bottom">
              <el-input
                placeholder="请输入项目名称"
                clearable
                v-model="filterField.projectName"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="区域" class="row-padding-bottom">
              <el-cascader
                ref="manageArea"
                v-model="areaId"
                :options="areasOptions"
                @change="getAreaId"
                clearable
                filterable
                :props="{ checkStrictly: true }"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="行业" class="row-padding-bottom">
                <el-select
                  v-model="filterField.industryId"
                  filterable
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="item in industryOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="投资类型" class="row-padding-bottom">
                <el-select
                  v-model="filterField.investment"
                  filterable
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="item in investmentOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="状态变更" class="row-padding-bottom">
                <el-select
                  v-model="filterField.newStatus"
                  filterable
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="item in statusOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="申请用户" class="row-padding-bottom">
                <el-input
                  placeholder="请输入申请用户名称"
                  clearable
                  v-model="filterField.applyUserName"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="申请日期" class="row-padding-bottom">
                <timeSelection
                  @getStartTimeValue="getStartTimeValue"
                  @getEndTimeValue="getEndTimeValue"
                ></timeSelection>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="审核状态" class="row-padding-bottom">
                <el-select
                  v-model="filterField.checkStatus"
                  filterable
                  placeholder="请选择"
                  clearable
                >
                  <el-option label="待审核" value="AUDITING"></el-option>
                  <el-option label="审核通过" value="PASSED"></el-option>
                  <el-option label="审核不通过" value="REJECTED"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </template>
          <el-col :lg="8" :xl="6">
            <el-form-item>
              <a
                @click="toggleAdvanced"
                style="margin-left: 40px; color: #409eff; cursor: pointer"
                class="btnColor"
              >
                {{ advanced ? '收起' : '展开' }}
                <i
                  :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                ></i>
              </a>

              <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchTable"
                class="searchBtn"
              >
                <span>搜索</span>
              </el-button>
            </el-form-item>
            <el-form-item>
              <exportBtn
                :responseType="`get`"
                :dataLen="tableData.length"
                :filterField="filterField"
                api="api-p/projectStatusCheck/expand/export"
              ></exportBtn>
              <!-- <el-button type="primary" icon="icon iconfont icondaochu" @click="exportExcel" class="commonBtn saveBtn funComBut">导出</el-button> -->
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          :data="tableData"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column prop="projectName" label="项目名称" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.projectName"
                :columnName="'projectName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="oldToNewStatus"
            label="状态变更申请"
            min-width="150"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="applyUserName"
            label="申请用户"
            width="130"
            :formatter="formatTd"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.applyUserName"
                :columnName="'applyUserName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="applyTime"
            label="申请日期"
            min-width="130"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="reason"
            label="变更原因说明"
            width="180"
            :formatter="formatTd"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.reason"
                :columnName="'reason'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            label="附件"
            align="left"
            width="180"
            :formatter="formatTd"
          >
            <template slot-scope="scope">
              <el-link
                type="primary"
                v-if="scope.row.files.length"
                @click="getUploadFile(scope.row.files)"
                >查看</el-link
              >
              <span v-else>--</span>
            </template>
          </el-table-column>
          <el-table-column
            label="审核状态"
            prop="checkStatusStr"
            align="left"
            width="120"
            :formatter="formatTd"
          >
            <template slot-scope="scope">
              <span
                v-if="scope.row.checkStatusStr == '审核通过'"
                style="color: #06ac15"
                >{{ scope.row.checkStatusStr }}</span
              >
              <span
                v-else-if="scope.row.checkStatusStr == '审核不通过'"
                style="color: #e31c12"
                >{{ scope.row.checkStatusStr }}</span
              >
              <span v-else style="color: #ffac37">{{
                scope.row.checkStatusStr
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="left"
            width="240"
            :formatter="formatTd"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-link
                type="primary"
                v-if="scope.row.canCheck"
                @click="showAudit(scope.row.id, 'SENDING')"
                >审核通过</el-link
              >
              <el-link
                type="primary"
                v-if="scope.row.canCheck"
                @click="showDialog(scope.row.id, 'AUDITFAILED')"
                >驳回</el-link
              >
              <el-link type="primary" @click="getDetail(scope.row.id)"
                >查看详情</el-link
              >
            </template>
          </el-table-column>
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无数据"></emptyTable>
          </div>
        </el-table>
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
        ></Pagination>
      </div>
    </div>
    <!-- 审核/驳回弹窗 -->
    <el-dialog
      :title="dialogTitle"
      :visible="dialogVisible"
      @close="closeDialog"
      custom-class="dialog-minwidth"
      class="tipDialog"
      width="420px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :modal-append-to-body="false"
    >
      <el-form
        v-if="ruleForm.status === 'AUDITFAILED'"
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="驳回原因" prop="rejectReason">
          <el-input
            type="textarea"
            :rows="3"
            :maxlength="300"
            placeholder="请输入驳回原因"
            show-word-limit
            v-model="ruleForm.rejectReason"
          ></el-input>
        </el-form-item>
      </el-form>
      <!-- <p v-else>确认通过审核？</p> -->
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="closeDialog" class="cancel">取 消</el-button>
        <el-button
          type="primary"
          v-sdc
          class="save"
          @click.native="updateStatus('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <contractEnclosureDialog
      v-if="uploadContract"
      dialogTitle="附件"
      :tableData="uploadImg"
      @closeDialog="uploadContract = false"
    ></contractEnclosureDialog>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  name: 'projectStatusCheck',
  data() {
    return {
      advanced: false,
      areaId: '',
      isShow: true,
      requestUrl: 'api-p/projectStatusCheck/expand/list', //表格请求路径
      filterField: {
        projectName: '',
        areaId: '',
        industryId: '',
        investment: '',
        newStatus: '',
        applyUserName: '',
        applyDateStart: '',
        applyDateEnd: '',
        checkStatus: '',
      },
      tableData: [],
      dialogTitle: '',
      dialogVisible: false,
      ruleForm: {
        id: '',
        status: '',
        rejectReason: '',
      },
      rules: {
        rejectReason: {
          required: true,
          message: '请输入驳回原因',
          trigger: 'blur',
        },
      },
      uploadImg: [],
      srcList: [],
      oldSrcList: [],
      uploadContract: false,
    }
  },
  computed: {
    ...mapState([
      'investmentOptions',
      'statusOptions',
      'areasOptions',
      'industryOptions',
    ]),
  },
  created() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
  },
  methods: {
    //获取选中的区域最后一级id
    getAreaId() {
      let areaId = this.areaId[this.areaId.length - 1]
      this.filterField.areaId = areaId
    },
    getStartTimeValue(val) {
      if (val) {
        this.filterField.applyDateStart = val
      } else {
        this.filterField.applyDateStart = ''
      }
    },
    getEndTimeValue(val) {
      if (val) {
        this.filterField.applyDateEnd = val
      } else {
        this.filterField.applyDateEnd = ''
      }
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getTableList(data) {
      this.tableData = data
      this.loading = false
      if (this.tableData.length < 1) {
        this.isShow = true
      }
    },
    //列表查询
    searchTable() {
      this.$refs.pagination.getTableListData()
    },
    //查看附件
    getUploadFile(item) {
      const results = item
      this.uploadImg = []
      this.srcList = []
      this.oldSrcList = []
      this.uploadContract = true
      this.uploadImg = item
      for (var i = 0; i < this.uploadImg.length; i++) {
        this.srcList.push(this.uploadImg[i].absolutePath)
        this.oldSrcList.push(this.uploadImg[i].absolutePath)
      }
    },
    //查看详情
    getDetail(id) {
      console.log(id)
      this.$router.push({
        path: '/home/project/projectStatusCheckDetail',
        name: 'projectStatusCheckDetail',
        query: {
          id,
          activeName: 'second',
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/project/projectChangeRecord',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    // 审核通过/驳回弹窗操作
    showDialog(id, status) {
      this.ruleForm.id = id
      this.ruleForm.status = status
      if (status === 'SENDING' || status === 'JCAUDITING') {
        this.dialogTitle = '审核确认'
      } else {
        this.dialogTitle = '驳回'
      }
      this.dialogVisible = true
    },
    closeDialog() {
      this.dialogVisible = false
      if (this.ruleForm.status === 'AUDITFAILED') {
        this.$refs.ruleForm.resetFields()
      }
    },
    updateStatus(formName) {
      if (this.ruleForm.status === 'AUDITFAILED') {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.submitStatus()
          } else {
            this.$message.error('请按照提示规则输入内容!')
            return false
          }
        })
      } else {
        this.submitStatus()
      }
    },
    submitStatus() {
      let data = {
        id: this.ruleForm.id,
        checkStatus:
          this.ruleForm.status === 'AUDITFAILED' ? 'REJECTED' : 'PASSED',
      }
      if (this.ruleForm.status === 'AUDITFAILED') {
        Object.assign(data, { rejectReason: this.ruleForm.rejectReason })
      }
      return this.axios({
        method: 'post',
        url: 'api-p/projectStatusCheck/expand/audit',
        data,
      })
        .then((response) => {
          if (!response.data.hasError) {
            this.$message.success('操作成功')
            this.searchTable()
          } else {
            this.$message.error(response.data.errorMsg)
          }
          this.dialogVisible = false
        })
        .catch((error) => {
          this.dialogVisible = false
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    // 审核改为提示框
    showAudit(id, status) {
      this.$confirm('确认通过审核?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.ruleForm.id = id
          this.ruleForm.status = status
          this.submitStatus()
        })
        .catch(() => {
          // // this.$message.error("删除失败");
        })
    },
  },
}
</script>

<style></style>
